package com.zxm.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zxm.pojo.UserInfo;
import com.zxm.service.UserService;

@Controller
@RequestMapping("/userLogin")
public class UserLoginController {
	@Reference
	UserService userService;

	@RequestMapping("/login")
	@ResponseBody
	public Map<String, String> usrlogin(UserInfo userInfo) {
		HttpSession session = getSession();
		Map<String, String>map=new HashMap<>();
		UserInfo user = new UserInfo();
		try {
			user = userService.userLogin(userInfo);
			System.out.println(user);
			if (null != user) {
				session.setAttribute("userInfo", user);
				map.put("page", "index");
				return map;
			}
		} catch (Exception e) {
			map.put("page", "page-login");
			return map;
		}
		return map;
	}

	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}
}
